package kugou

import (
	"api-go/serializer"
	"api-go/util"
	"net/url"
)

// 搜索所需信息
type Search struct {
	Key    string `form:"key"`
	Limit  string `form:"limit"`
	Offset string `form:"offset"`
	Type   string `form:"type"`
}

//获取搜索信息
func (service *Search) SearchInfo() *serializer.Response {

	if val := service.Key; val != "" {
		service.Key = val
	} else {
		service.Key = "周杰伦"
	}

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "30"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	// 关键字搜索（song: 单曲, alnum: 专辑, special: 歌单, mv: MV）
	if val := service.Type; val != "" {
		service.Type = val
	} else {
		service.Type = "1"
	}
	//1: 单曲, 2: 专辑,  5: MV, 9：歌单
	switch service.Type {
	case "1":
		service.Type = "song"
	case "2":
		service.Type = "album"
	case "5":
		service.Type = "mv"
	case "9":
		service.Type = "special"
	}

	//此处需要对中文进行转码，否则会无法请求第三方api
	service.Key = url.QueryEscape(service.Key)
	url := "http://mobilecdn.kugou.com/api/v3/search/" + service.Type + "?format=json&keyword=" + service.Key + "&page=" + service.Offset + "&pagesize=" + service.Limit + "&showtype=1"

	// fmt.Println(url)
	res, err := util.Get(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取热搜
func (service *Search) SearchHot() *serializer.Response {

	url := "https://searchrecommend.kugou.com/v1/word_nofocus"

	res, err := util.Get(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}
